﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admintool.ExtensionMethods
{
    public static class IEnumerableExtension
    {

        public static void ForEach(this IEnumerable enumeration, Action action)
        {
            foreach (var item in enumeration)
            {
                action();
            }

        }


        public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
        {
            foreach (var item in enumeration)
            {
                action(item);
            }

        }

        public static void ForEach<T>(this IEnumerable<T> enumeration, Action<int, T> action)
        {
            int index = 0;
            var iterator = enumeration.GetEnumerator();
            while (iterator.MoveNext())
            {

                T val = iterator.Current;
                action(index, val);
                index++;
            }

        }


        public static T Value<T>(this T[] array, int index)
        {
            return array!=null && index>=0 && array.Length>index ? (T) array[index]:default(T);

        }


        public static IEnumerable<String> ReadFromFile(String fileName)
        {
            String line;
            using (var file = System.IO.File.OpenText(fileName))
            {
                // read each line, ensuring not null (EOF)
                while ((line = file.ReadLine()) != null)
                {
                    // return trimmed line
                    yield return line.Trim();
                }
            }
        }

    }
}
